package com.gis.school.controller;

import com.gis.school.domain.User;
import com.gis.school.service.UserService;
import com.gis.school.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @PostMapping("/login")
    public Result<User> loginController(@RequestParam String uname, @RequestParam String password){
        User user = userService.loginService(uname, password);
        if(user!=null){
            logger.info("登录成功: {}", user);
            return Result.success(user,"登录成功！");
        }else{
            logger.warn("账号或密码错误: {} {}", uname, password);
            return Result.error("123","账号或密码错误！");
        }
    }

    @PostMapping("/register")
    public Result<User> registController(@RequestBody User newUser){
        User user = userService.registService(newUser);
        if(user != null){
            logger.info("注册成功: {}", user);
            return Result.success(user,"注册成功！");
        }else{
            logger.warn("用户名已存在: {}", newUser.getUname());
            return Result.error("456","用户名已存在！");
        }
    }
}
